package com.scu.phototabuser.data.source.remote.login;

import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.test.espresso.core.deps.guava.base.Objects;
import android.support.test.espresso.core.deps.guava.base.Strings;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scu.phototabuser.Consts;
import com.scu.phototabuser.data.Taskable;
import com.scu.phototabuser.data.source.local.Token;
import com.scu.phototabuser.data.source.local.User;
import com.scu.phototabuser.data.source.remote.Response;
import com.scu.phototabuser.http.OkHttp3Methods;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.RequestBody;

import static com.scu.phototabuser.util.Predictions.checkNotNull;
/**
 * Created by tianfei on 2017/4/9.
 */

public abstract class LoginTaskable extends Taskable{


    private User mUser;
    private Token token;
    private boolean result=false;
    private Response response;



    public LoginTaskable(@NonNull String email, @NonNull String MD5pass) {
        mUser=new User();
        mUser.setEmail(email);
        mUser.setPasswordMD5(MD5pass);
    }

    @Override
    public boolean isSucceed() {
        return this.result;
    }

    @Override
    public abstract void onFailed(Integer code, String msg);

    @Override
    public abstract void onSuccess(Object data);

    //当isSucceed成功后可以获取token
    public Token getToken(){
        if (result){
            return token;
        }else {
            return null;
        }
    }

    //实现登录用户
    @Override
    public void start(TASK_TYPE type) {

        switch (type){
            case LOGIN:
                new LoginAsyncTask(TASK_TYPE.LOGIN).execute(mUser);
                break;
            case SINGUP:
                new LoginAsyncTask(TASK_TYPE.SINGUP).execute(mUser);
                break;
            default:break;
        }

    }

    private class LoginAsyncTask extends AsyncTask<User,Integer,Object> {

        TASK_TYPE type;

        LoginAsyncTask(TASK_TYPE type) {
            super();
            this.type=type;
        }

        @Override
        protected Object doInBackground(User... params) {

            //String name="";
            String url="";
            if (type==TASK_TYPE.SINGUP){
                //name=params[0].getName();
                url=Consts.SignUpURL;
            }else {
                url=Consts.LoginURL;
            }

            String _email=params[0].getEmail();
            String _MD5pass=params[0].getPasswordMD5();

            Map<String,String> httpBodyParams = new HashMap<>();

            httpBodyParams.put("email",_email);
            httpBodyParams.put("MD5pass",_MD5pass);
                //httpBodyParams.put("name",name);


            String res="";
            try {
                res=
                OkHttp3Methods.UniversalPostRequest(url,httpBodyParams);

                Log.e("eeeeeeeee", "doInBackground: "+res);

                Type type=new TypeToken<Response<Token>>(){}.getType();
                response=new Gson().fromJson(res,type);

                if (response.getCode().equals(200)){
                    result=true;
                    return response.getData();
                }else {
                    return null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                result=false;
                response =new Response<Void>(-1,Consts.UNKNOWN_ERR,null);
                return null;
            }
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);

            if (result){
                token=(Token) o;
                onSuccess(response.getData());
            } else {
                onFailed(response.getCode(),response.getMessage());
            }
        }
    }
}

